#include "sht20.h"
#include "main.h"

#if 1
SHT2x_data SHT20;

void sht20_test_task(void *pvParameter);

uint8_t sht20_write_len(uint8_t regAddr,uint8_t val)
{

	uint8_t ret=0;
    uint8_t write_buf[] = {regAddr, val};

    ret = i2c_master_write_to_device(I2C1_PORT_NUM, SHT20_ADR, write_buf, sizeof(write_buf), pdMS_TO_TICKS(I2C_MASTER_TIMEOUT_MS));
	if(ret == ESP_OK)
	{
		ret = 0;
		bsp_i2c_debug("写入成功");
	}else
	{
		ret = 1;
		bsp_i2c_debug("写入失败");
	}
    return ret;
}

uint16_t sht20_read_reg(uint8_t regAddr)
{
    uint8_t read_buf[2]={0,0};
    uint8_t data=regAddr;

	if(i2c_master_write_read_device(I2C1_PORT_NUM, SHT20_ADR, &data, 1, read_buf, 2, pdMS_TO_TICKS(I2C_MASTER_TIMEOUT_MS))==ESP_OK)
	{
		bsp_i2c_debug("读取成功 地址:0x%02x 数据:0x%02x",regAddr,read_buf[0]<<8|read_buf[1]);
	}else
	{
		bsp_i2c_debug("读取失败");
	}

	return (uint16_t)(read_buf[0]<<8)|read_buf[1];
}





uint8_t sht20_init(void)
{
	uint8_t err;
	err = sht20_softreset();
	if(err==1)
    {
        sht20_debug("SHT20初始化失败!\r\n");
    }
	else
    {

        sht20_debug("SHT20初始化成功!\r\n");
        xTaskCreate(sht20_test_task,"sht20_test_task",1024*2,NULL,2,NULL);
    }

	return err;
}

void sht20_test_task(void *pvParameter)
{
	uint8_t vol=0;
	while (1)
    {

        sht20_debug("温度:%d\r\n",sht20_gettemppoll());
        sht20_debug("湿度:%d\r\n",sht20_gethumipoll());

		vTaskDelay(1000 / portTICK_PERIOD_MS);
 	}
	vTaskDelete(NULL);
}

uint8_t sht20_softreset(void) //SHT20软件复位
{
	uint8_t ret=0;
    ret = sht20_write_len(0x80,0xfe);
	return ret;
}
int sht20_gettemppoll(void)
{
    float TEMP=0.0;
    uint16_t ST=0;

    ST = sht20_read_reg(TRIG_TEMP_MEASUREMENT_POLL);

    ST &= ~0x0003;
    TEMP = ((float)ST * 0.00268127) - 46.85;
    return (int)(TEMP*100);
}

int sht20_gethumipoll(void)
{
    float HUMI=0.0;
    uint16_t SRH=0;

    SRH = sht20_read_reg(TRIG_HUMI_MEASUREMENT_POLL);

    SRH &= ~0x0003;
    HUMI = ((float)SRH * 0.00190735) - 6;

    return (int)(HUMI*100);
}


#else

SHT2x_data SHT20;
uint8_t sht20_init(void)
{
	uint8_t err;
    i2c_init();

	err = sht20_softreset();
	if(err!=ESP_OK)
		printf("SHT20错误!!!\r\n");
	else
		printf("SHT20就绪!!!\r\n");

	return err;
}


uint8_t sht20_softreset(void) //SHT20软件复位
{
	esp_err_t ret =0;

    i2c_cmd_handle_t cmd = i2c_cmd_link_create();                                   //新建操作I2C句柄
    i2c_master_start(cmd);                                                          //启动I2C
    i2c_master_write_byte(cmd, ( SHT20_ADR << 1 ) | WRITE_BIT, ACK_CHECK_EN);    //发地址+写+检查ack
    ret = i2c_master_write_byte(cmd, SOFT_RESET, ACK_CHECK_EN);    //发地址+写+检查ack
    i2c_master_stop(cmd);                                                           //停止I2C
    ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_PERIOD_MS);        //I2C发送
    i2c_cmd_link_delete(cmd);     //删除I2C句柄
    return ret;
}


int sht20_gettemppoll(void)
{
    float TEMP;
    uint8_t ack, tmp1, tmp2,tmp3;
    uint16_t ST;
    uint16_t i=0;
    esp_err_t ret=0;

    i2c_cmd_handle_t cmd = i2c_cmd_link_create();                                   //新建操作I2C句柄
    i2c_master_start(cmd);                                                          //启动I2C
    i2c_master_write_byte(cmd, ( SHT20_ADR << 1 ) | WRITE_BIT, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, TRIG_TEMP_MEASUREMENT_POLL, ACK_CHECK_EN);
    i2c_master_start(cmd);
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, ( SHT20_ADR << 1 ) | READ_BIT, ACK_CHECK_EN);
    i2c_master_read_byte(cmd, &tmp1, ACK_VAL);
    i2c_master_read_byte(cmd, &tmp2, ACK_VAL);
    i2c_master_stop(cmd);                                         //停止I2C
    ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_PERIOD_MS);        //I2C发送
    i2c_cmd_link_delete(cmd);     //删除I2C句柄
    if (ret != ESP_OK) {
        printf("SHT20错误\r\n");
        return ret;
    }


    ST = (tmp1 << 8) | (tmp2 << 0);
    ST &= ~0x0003;
    TEMP = ((float)ST * 0.00268127) - 46.85;
    return (uint16_t)(TEMP*100);
}

#endif
